﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Package_EditPackageType : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (isLogin())
        {
            if (LoginedStaff.tblRole.Role != "Admin")
                Response.Redirect("~/Admin/Index.aspx");

            if (!IsPostBack)
            {
                MailDistributionSystemDBDataContext db = new MailDistributionSystemDBDataContext();
                try
                {
                    if (Request.QueryString["id"] != null)
                    {
                        int id = Convert.ToInt16(Request.QueryString["id"]);

                        tblParcelType parcel = db.tblParcelTypes.Where(c => c.ID == id).SingleOrDefault();
                        ParcelType.Text = parcel.ParcelType;
                        AdditionalFee.Text = Convert.ToString(parcel.AdditionalFee);
                    }
                    else
                    {
                        Response.Redirect("~/Admin/Package/ViewParcelType.aspx");
                    }
                }
                catch { Response.Redirect("~/Admin/Package/ViewParcelType.aspx"); }
            }
        }
        else
        {
            Response.Redirect("~/Admin/Stafflogin.aspx");
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        nameLabel.Visible = false;
        feeLabel.Visible = false;
        decimal dd;
        bool feeCheck = Decimal.TryParse(AdditionalFee.Text, out dd);
        if (string.IsNullOrEmpty(AdditionalFee.Text) || Convert.ToDecimal(AdditionalFee.Text) <= 0)
        {
            feeCheck = false;
        }
        if (!string.IsNullOrEmpty(ParcelType.Text) && feeCheck)
        {
            MailDistributionSystemDBDataContext db = new MailDistributionSystemDBDataContext();
            int id = Convert.ToInt16(Request.QueryString["id"]);
            tblParcelType parcel = db.tblParcelTypes.Where(c => c.ID == id).SingleOrDefault();
            tblParcelType tmp = db.tblParcelTypes.SingleOrDefault(c => c.ParcelType == ParcelType.Text.Trim());
            if (tmp != null && tmp.ID != id)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Parcel Type Already Exist!')", true);
            }
            else
            {
                parcel.ParcelType = ParcelType.Text.TrimEnd();
                parcel.AdditionalFee = Convert.ToDecimal(AdditionalFee.Text);
                db.SubmitChanges();
                Response.Redirect("~/Admin/ParcelType/ViewParcelType.aspx");
            }
        }
        else
        {
            if (string.IsNullOrEmpty(ParcelType.Text))
            {
                nameLabel.Visible = true;
            }
            if (!feeCheck)
            {
                feeLabel.Visible = true;
            }
        }
    }
}